public class double[]
extends Object
GDK enhancements for double[].
| Type Params | Return Type | Name and description |
|---|---|---|
|
public boolean |
any(Closure<?> predicate)Iterates over the contents of a double Array, and checks whether a predicate is valid for at least one element. |
|
public boolean |
asBoolean()Coerces a double array to a boolean value. |
|
public double |
average()Calculates the average of the doubles in the array. |
|
public List<List<Double>> |
chop(int chopSizes)Chops the double array into pieces, returning lists with sizes corresponding to the supplied chop sizes. |
|
public boolean |
contains(Object value)Checks whether the array contains the given value. |
|
public Number |
count(Object value)Counts the number of occurrences of the given value inside this array. |
|
public DoubleStream |
doubleStream()Returns a sequential DoubleStream with the specified array as its source. |
|
public double[] |
each(DoubleConsumer consumer)Iterates through a double[] passing each double to the given consumer. |
|
public double[] |
eachWithIndex(Closure<?> closure)Iterates through a double[], passing each double and the element's index (a counter starting at zero) to the given closure. |
|
public boolean |
equals(double[] right)Compares the contents of this array to the contents of the given array. |
|
public boolean |
every(Closure<?> predicate)Iterates over the contents of a double Array, and checks whether a predicate is valid for all elements. |
|
public double |
first()Returns the first item from the double array. |
|
public List<Double> |
flatten()Flattens an array. |
|
public List<Double> |
getAt(Range<?> range)Supports the subscript operator for a double array with a range giving the desired indices. |
|
public List<Double> |
getAt(IntRange range)Supports the subscript operator for a double array with an IntRange giving the desired indices. |
|
public List<Double> |
getAt(ObjectRange range)Supports the subscript operator for a double array with an ObjectRange giving the desired indices. |
|
public List<Double> |
getAt(Collection<?> indices)Supports the subscript operator for a double array with a (potentially nested) collection giving the desired indices. |
|
public IntRange |
getIndices()Returns indices of the double array. |
|
public String |
groovyToString()Returns Groovy's list-like string representation for a double array. |
|
public double |
head()Returns the first item from the double array. |
|
public Map<Integer, Double> |
indexed()Zips a double[] with indices in (index, value) order starting from index 0. |
|
public Map<Integer, Double> |
indexed(int offset)Zips a double[] with indices in (index, value) order starting from a given index. |
|
public double[] |
init()Returns the items from the double array excluding the last item. |
|
public String |
join()Concatenates the string representation of each item in this array. |
|
public String |
join(String separator)Concatenates the string representation of each item in this array, with the given String as a separator between each item. |
|
public double |
last()Returns the last item from the double array. |
|
public double |
max()Adds max() method to double arrays. |
|
public double |
max(DoubleComparator comparator)Selects the maximum value found from the double array using the supplied DoubleComparator to determine the maximum of any two values. |
|
public double |
max(DoubleUnaryOperator operator)Selects the maximum value found from the double array using the supplied DoubleUnaryOperator to determine the maximum of any two values. |
|
public double |
maxComparing(Comparator<Double> comparator)Selects the maximum value found from the double array using the comparator to determine the maximum of any two values. |
|
public double |
min()Adds min() method to double arrays. |
|
public double |
min(DoubleComparator comparator)Selects the minimum value found from the double array using the supplied DoubleComparator to determine the minimum of any two values. |
|
public double |
min(DoubleUnaryOperator operator)Selects the minimum value found from the double array using the supplied DoubleUnaryOperator to determine the minimum of any two values. |
|
public double |
minComparing(Comparator<Double> comparator)Selects the minimum value found from the double array using the comparator to determine the minimum of any two values. |
|
public int |
partitionPoint(IntRange range, DoublePredicate condition)Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). |
|
public int |
partitionPoint(DoublePredicate condition)Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). |
|
public void |
putAt(IntRange range, double[] source)A helper method to allow arrays to be set with subscript operators. |
|
public void |
putAt(IntRange range, Iterable<Number> source)A helper method to allow arrays to be set with subscript operators. |
|
public double[] |
reverse()Creates a new double array containing items which are the same as this array but in reverse order. |
|
public double[] |
reverse(boolean mutate)Reverses the items in an array. |
|
public double[] |
reverseEach(Closure<?> closure)Iterates through a double[] in reverse order passing each double to the given closure. |
|
public int |
size()Provides arrays with a size method similar to collections. |
|
public Stream<Double> |
stream()Returns a sequential Stream with the specified array as its source. |
|
public double |
sum()Sums the items in an array. |
|
public double |
sum(DoubleUnaryOperator operator)Calculates the sum of values found from applying the operator to members of the int array. |
|
public double |
sum(double initialValue)Sums the items in an array, adding the result to some initial value. |
|
public double[] |
swap(int i, int j)Swaps two elements at the specified positions. |
|
public double[] |
tail()Returns the items from the double array excluding the first item. |
|
public List<Double> |
toList()Converts this array to a List of the same size, with each element added to the list. |
|
public Set<Double> |
toSet()Converts this array to a Set, with each unique element added to the set. |
|
public String |
toString()Returns the string representation of the given array. |
|
public List<Tuple2<Double, Integer>> |
withIndex()Zips a double array with indices in (value, index) order. |
|
public List<Tuple2<Double, Integer>> |
withIndex(int offset)Zips a double array with indices in (value, index) order starting from a given index. |
|
public List<Tuple2<Double, Double>> |
zip(double[] other)A list of all the pairs from two arrays. |
|
public Iterator<Tuple2<Double, Double>> |
zipping(double[] other)An iterator of all the pairs from two arrays. |
| Methods inherited from class | Name |
|---|---|
class Object |
addShutdownHook, any, any, asBoolean, asType, collect, collect, collect, dump, each, eachMatch, eachMatch, eachWithIndex, every, every, find, find, findAll, findAll, findIndexOf, findIndexOf, findIndexValues, findIndexValues, findLastIndexOf, findLastIndexOf, findResult, findResult, findResult, findResult, getAt, getMetaClass, getMetaPropertyValues, getProperties, grep, grep, hasProperty, identity, inject, inject, inspect, invokeMethod, is, isCase, isNotCase, iterator, metaClass, print, print, printf, printf, println, println, println, putAt, respondsTo, respondsTo, setMetaClass, sleep, sleep, split, sprintf, sprintf, stream, tap, toString, use, use, use, with, with, withCloseable, withCloseable, withMethodClosure, withStream, withStream, withTraits |
Iterates over the contents of a double Array, and checks whether a predicate is valid for at least one element.
double[] array = [0.0d, 1.0d, 2.0d]
assert array.any{ it > 1.5d }
assert !array.any{ it > 2.5d }
predicate - the closure predicate used for matchingCoerces a double array to a boolean value. A double array is false if the array is of length 0, and true otherwise.
Calculates the average of the doubles in the array.
assert 5.0d == ([2,4,6,8] as double[]).average()
Chops the double array into pieces, returning lists with sizes corresponding to the supplied chop sizes. If the array isn't large enough, truncated (possibly empty) pieces are returned. Using a chop size of -1 will cause that piece to contain all remaining items from the array.
double[] array = [0, 1, 2]
assert array.chop(1, 2) == [[0], [1, 2]]
chopSizes - the sizes for the returned piecesChecks whether the array contains the given value.
value - the value being searched for Counts the number of occurrences of the given value inside this array.
Comparison is done using Groovy's == operator (using
compareTo(value) == 0).
double[] array = [10.0d, 20.0d, 20.0d, 30.0d]
assert array.count(20.0d) == 2
value - the value being searched forReturns a sequential DoubleStream with the specified array as its source.
Stream for the arrayIterates through a double[] passing each double to the given consumer.
double[] array = [0d, 1d, 2d]
String result = ''
array.each{ result += it }
assert result == '0.01.02.0'
consumer - the consumer for each doubleIterates through a double[], passing each double and the element's index (a counter starting at zero) to the given closure.
double[] array = [10d, 20d, 30d]
String result = ''
array.eachWithIndex{ item, index -> result += "$index($item)" }
assert result == '0(10.0)1(20.0)2(30.0)'
closure - a Closure to operate on each doubleCompares the contents of this array to the contents of the given array.
Example usage:
double[] array1 = [4.0d, 8.0d]
double[] array2 = [4.0d, 8.0d]
assert array1 !== array2
assert array1.equals(array2)
right - the array being comparedIterates over the contents of a double Array, and checks whether a predicate is valid for all elements.
double[] array = [0.0d, 1.0d, 2.0d]
assert array.every{ it < 2.5d }
assert !array.every{ it > 1.5d }
predicate - the closure predicate used for matchingReturns the first item from the double array.
double[] doubles = [10.0d, 20.0d, 30.0d]
assert doubles.first() == 10.0d
An alias for head(). Flattens an array. This array is added to a new collection.
It is an alias for toList() but allows algorithms to be written which also
work on multidimensional arrays or non-arrays where flattening would be applicable.
double[] array = [0.0d, 1.0d]
assert array.flatten() == [0.0d, 1.0d]
Supports the subscript operator for a double array with a range giving the desired indices.
double[] array = [1.0d, 3.0d, 5.0d, 7.0d, 9.0d, 11.0d]
assert array[2..<2] == [] // EmptyRange
assert array[(0..5.5).step(2)] == [1.0d, 5.0d, 9.0d] // NumberRange
assert array[(1..5.5).step(2)] == [3.0d, 7.0d, 11.0d] // NumberRange
range - a range indicating the indices for the items to retrieveSupports the subscript operator for a double array with an IntRange giving the desired indices.
double[] array = [0.0d, 10.0d, 20.0d, 30.0d, 40.0d]
assert array[2..3] == [20.0d, 30.0d]
assert array[-2..-1] == [30.0d, 40.0d]
assert array[-1..-2] == [40.0d, 30.0d]
range - an IntRange indicating the indices for the items to retrieveSupports the subscript operator for a double array with an ObjectRange giving the desired indices.
double[] array = [0.0d, 10.0d, 20.0d, 30.0d, 40.0d]
def range = new ObjectRange(2, 3)
assert array[range] == [20.0d, 30.0d]
range - an ObjectRange indicating the indices for the items to retrieveSupports the subscript operator for a double array with a (potentially nested) collection giving the desired indices.
double[] array = [0.0d, 2.0d, 4.0d, 6.0d, 8.0d]
assert array[2, 3] == [4.0d, 6.0d]
assert array[1, 0..1, [0, [-1]]] == [2.0d, 0.0d, 2.0d, 0.0d, 8.0d]
indices - a collection of indices for the items to retrieveReturns indices of the double array.
double[] array = [0.0d, 1.0d]
assert array.indices == 0..1
Returns Groovy's list-like string representation for a double array.
If disabled, e.g. via -Dgroovy.extension.disable=groovyToString(double[]),
the JDK's default array toString() is used instead.
Returns the first item from the double array.
double[] doubles = [10.0d, 20.0d, 30.0d]
assert doubles.head() == 10.0d
An alias for first().Zips a double[] with indices in (index, value) order starting from index 0.
Example usage:
double[] nums = [10.0d, 20.0d, 30.0d]
assert [0: 10.0d, 1: 20.0d, 2: 30.0d] == nums.indexed()
Zips a double[] with indices in (index, value) order starting from a given index.
Example usage:
double[] nums = [10.0d, 20.0d, 30.0d]
assert [5: 10.0d, 6: 20.0d, 7: 30.0d] == nums.indexed(5)
offset - an index to start fromReturns the items from the double array excluding the last item.
double[] doubles = [10.0d, 20.0d, 30.0d]
def result = doubles.init()
assert result == [10.0d, 20.0d]
assert doubles.class.componentType == result.class.componentType
Concatenates the string representation of each item in this array.
Concatenates the string representation of each item in this array, with the given String as a separator between each item.
separator - a String separatorReturns the last item from the double array.
double[] doubles = [10.0d, 20.0d, 30.0d]
assert doubles.last() == 30.0d
Adds max() method to double arrays.
Example usage:
double[] nums = [1.1d, 3.3d, 2.2d]
assert 3.3d == nums.max()
Selects the maximum value found from the double array using the supplied DoubleComparator to determine the maximum of any two values.
double[] nums = [10d, 20d, -30d]
assert 20d == nums.max{ n, m -> n <=> m }
assert -30d == nums.max{ n, m -> n.abs() <=> m.abs() }
comparator - a comparator, i.e. returns a negative value if the first parameter is less than the secondSelects the maximum value found from the double array using the supplied DoubleUnaryOperator to determine the maximum of any two values. The operator is applied to each array element and the results are compared.
double[] nums = [10d, 20d, -30d]
assert -30d == nums.max{ it.abs() }
assert 20d == nums.max{ it }
operator - an operator that returns a double used for comparing valuesSelects the maximum value found from the double array using the comparator to determine the maximum of any two values.
double[] nums = [10.0d, 20.0d, 30.0d]
assert 30d == nums.maxComparing(Comparator.naturalOrder())
assert 10d == nums.maxComparing(Comparator.reverseOrder())
comparator - a ComparatorAdds min() method to double arrays.
Example usage:
double[] nums = [20.0d, 10.0d, 30.0d]
assert 10.0d == nums.min()
Selects the minimum value found from the double array using the supplied DoubleComparator to determine the minimum of any two values.
double[] nums = [10d, -20d, 30d]
assert -20d == nums.min{ n, m -> n <=> m }
assert 10d == nums.min{ n, m -> n.abs() <=> m.abs() }
comparator - a comparator, i.e. returns a negative value if the first parameter is less than the secondSelects the minimum value found from the double array using the supplied DoubleUnaryOperator to determine the minimum of any two values. The operator is applied to each array element and the results are compared.
double[] nums = [10d, -20d, 30d]
assert -20d == nums.min{ it }
assert 10d == nums.min{ it.abs() }
operator - an operator that returns a double used for comparing valuesSelects the minimum value found from the double array using the comparator to determine the minimum of any two values.
double[] nums = [10.0d, 20.0d, 30.0d]
assert 10d == nums.minComparing(Comparator.naturalOrder())
assert 30d == nums.minComparing(Comparator.reverseOrder())
comparator - a ComparatorReturns the index of the partition point according to the given predicate (the index of the first element of the second partition). The arr is assumed to be partitioned according to the given predicate.
def arr = [7, 15, 3, 5, 4, 12, 6] as double[]
assert arr.partitionPoint(0..<arr.size()) { it%2 != 0 } == 4
def arr = [1, 2, 3, 3, 4, 4, 5, 6, 7] as double[]
// usage case like lower_bound(cpp), bisect_left(python)
assert arr.partitionPoint(0..<arr.size()) { it < 4 } == 4
// usage case like upper_bound(cpp), bisect_right(python)
assert arr.partitionPoint(0..<arr.size()) { it <= 4 } == 6
// for all match condition
assert arr.partitionPoint(0..<arr.size()) { it <= 100 } == arr.size()
// for all match condition
assert arr.partitionPoint(0..<arr.size()) { it <= 0 } == 0
// for no match condition with range
assert arr.partitionPoint(2..<arr.size()) { it <= 0 } == 2
range - the range [l,r] to find data match the conditioncondition - the matching conditionReturns the index of the partition point according to the given predicate (the index of the first element of the second partition). The arr is assumed to be partitioned according to the given predicate.
def arr = [7, 15, 3, 5, 4, 12, 6] as double[]
assert arr.partitionPoint{ it%2 != 0 } == 4
def arr = [1, 2, 3, 3, 4, 4, 5, 6, 7] as double[]
// usage case like lower_bound(cpp), bisect_left(python)
assert arr.partitionPoint{ it < 4 } == 4
// usage case like upper_bound(cpp), bisect_right(python)
assert arr.partitionPoint{ it <= 4 } == 6
// for all match condition
assert arr.partitionPoint{ it <= 100 } == arr.size()
// for no match condition
assert arr.partitionPoint{ it <= 0 } == 0
condition - the matching conditionA helper method to allow arrays to be set with subscript operators.
double[] from = [1d, 1d, 1d]
double[] to = [0d, 0d, 0d, 0d, 0d]
to[1..3] = from
assert to == [0d, 1d, 1d, 1d, 0d]
range - the subset of the array to setsource - the source array from which new values will comeA helper method to allow arrays to be set with subscript operators. Zero will be used if the source iterable has insufficient elements.
def from = [1d, 1d, 1d]
double[] to = [0d, 0d, 0d, 0d, 0d]
to[1..3] = from
assert to == [0d, 1d, 1d, 1d, 0d]
range - the subset of the array to setsource - the source array from which new values will comeCreates a new double array containing items which are the same as this array but in reverse order.
double[] array = [1d, 2d]
assert array.reverse() == [2d, 1d]
Reverses the items in an array. If mutate is true, the original array is modified in place and returned. Otherwise, a new array containing the reversed items is produced.
double[] array = 1d..3d
def yarra = array.reverse(true)
assert array == 3d..1d
assert yarra == 3d..1d
assert array === yarra
yarra = array.reverse(false)
assert array !== yarra
assert array == 3d..1d
assert yarra == 1d..3d
mutate - true if the array itself should be reversed in place, false if a new array should be createdIterates through a double[] in reverse order passing each double to the given closure.
double[] array = [0, 1, 2]
String result = ''
array.reverseEach{ result += it }
assert result == '2.01.00.0'
closure - the closure applied on each double Provides arrays with a size method similar to collections.
Returns a sequential Stream with the specified array as its source.
Stream for the arraySums the items in an array.
assert (1+2+3+4 as double) == ([1,2,3,4] as double[]).sum()
Calculates the sum of values found from applying the operator to members of the int array.
int[] nums = [10, -20, 30]
assert 20 == nums.sum{ n -> n }
assert 60 == nums.sum{ n -> n.abs() }
operator - an operator that returns a double used for summing valuesSums the items in an array, adding the result to some initial value.
assert (5+1+2+3+4 as double) == ([1,2,3,4] as double[]).sum(5)
initialValue - the items in the array will be summed to this initial valueSwaps two elements at the specified positions.
Example:
assert ([1, 3, 2, 4] as double[]) == ([1, 2, 3, 4] as double[]).swap(1, 2)
i - a positionj - a positionReturns the items from the double array excluding the first item.
double[] doubles = [10.0d, 20.0d, 30.0d]
def result = doubles.tail()
assert result == [20.0d, 30.0d]
assert doubles.class.componentType == result.class.componentType
Converts this array to a List of the same size, with each element added to the list.
Converts this array to a Set, with each unique element added to the set.
double[] array = [1.0d, 2.0d, 3.0d, 2.0d, 1.0d]
Set expected = [1.0d, 2.0d, 3.0d]
assert array.toSet() == expected
Returns the string representation of the given array.
double[] array = [1, 2, 3, 2, 1]
assert array.toString() == '[1.0, 2.0, 3.0, 2.0, 1.0]'
Zips a double array with indices in (value, index) order.
Example usage:
double[] nums = [42.0d, -1.0d]
assert [[42.0d, 0], [-1.0d, 1]] == nums.withIndex()
assert ["0: 42.0", "1: -1.0"] == nums.withIndex().collect { n, idx -> "$idx: $n" }
Zips a double array with indices in (value, index) order starting from a given index.
Example usage:
double[] nums = [42.0d, -1.0d]
assert [[42.0d, 5], [-1.0d, 6]] == nums.withIndex(5)
assert ["5: 42.0", "6: -1.0"] == nums.withIndex(5).collect { n, idx -> "$idx: $n" }
offset - an index to start fromA list of all the pairs from two arrays.
double[] small = [1.0d, 2.0d, 3.0d]
double[] large = [100d, 200d, 300d]
assert [small, large].transpose() == small.zip(large)
other - another double[]An iterator of all the pairs from two arrays.
double[] small = [1.0d, 2.0d, 3.0d]
double[] large = [100d, 200d, 300d]
assert [small, large].transpose() == small.zipping(large).toList()
other - another double[]